<?php
/**
 * Trace.php
 * 轨迹
 * @author  :  Skiychan <dev@skiy.net>
 * @link    :  http://www.zzzzy.com
 * @created :  2015/12/21
 * @modified:
 * @version :  0.0.1
 */

require(APPPATH.'libraries/Web_Controller.php');
class Trace extends Web_Controller {
    private $_user_info = null;  //用户信息
    private $_trace_field = "id,actor,event,start,end,relation,ts,unsafe,hidden,file_id,actor_ip,actor_city";

    CONST rows = 10;

    public function __construct() {
        parent::__construct();
        $services = array(
            'login_service',
            'user_files_service',
            'file_trace_service'
        );
        $this->load->service($services);

        $this->_user_info = $this->login_service->get_user_info();
    }

    /**
     * 文件列表
     */
    public function lists_get() {
        #当前激活的id
        $id = trim($this->input->get('id', true));

        //主页轨迹
        $files = $this->file_trace_service->get_trace_files($this->_user_info['id'], array("deleted"=>0), "ts","desc","id,name",array(0, self::rows), $id);

        if (! empty($files)) {
            $fid = $files['rows'][0]['id'];

            $traces = $this->from_id($fid);

            if (! empty($traces)) {

                $traces['id'] = $fid;
                $res = array(
                    'files' => $files['rows'],
                    'active' => $traces
                );
                return $this->common_response(0, '', array('data' => $res));
            }
        }

        return $this->common_response(7008);
    }

    /**
     * 翻页 - 更多文件
     */
    public function more_get() {
        //页码
        $page = (int)$this->input->get("page",true);

        $files = $this->file_trace_service->get_trace_files($this->_user_info['id'], array("deleted"=>0),"ts","desc","id,name",array($page * self::rows, self::rows));

        if (! empty($files)) {
            $res = array('files' => $files['rows']);
            return $this->common_response(0, '', array('data' => $res));
        }

        return $this->common_response(7008);
    }

    /**
     * 根据ID获取轨迹日期、轨迹详情
     */
    public function file_get() {
        $id = (int)$this->input->get('id', true);

        if (empty($id))
            return $this->common_response(1003);

        $data = $this->from_id($id);
        if (empty($data))
            return $this->common_response(7008);

        return $this->common_response(0, '', array('data' => $data));
    }

    /**
     * 根据日期和文件id获取轨迹
     */
    public function date_get() {
        $id = (int)$this->input->get('id', true);
        $date = $this->input->get('date', true);

        if (empty($id) || empty($date))
            return $this->common_response(1003);

        $traces = $this->from_date($id, $date);
        #没有轨迹
        if (empty($traces))
            return $this->common_response(7008);

        return $this->common_response(0,'', array('data' => $traces));

    }

    /**
     * @param $id
     * @return array|void
     */
    private function from_id($id) {
        //取得日期列表
        $date_rows = $this->file_trace_service->get_dates($id,"ts","desc");

        //没有轨迹
        if (empty($date_rows))
            return;

        $dates = $this->date_format($date_rows['rows']);
        $condition = array("hidden"=>0,"date"=>$dates[0]['ts']);
        $traces_data = $this->file_trace_service->get_traces($id, $condition, $this->_trace_field);

        $traces = $this->file_trace_service->traces_format($traces_data['rows'],0);

        $data = array(
            'dates'      => $dates,
            'traces'     => $traces,
            'dateTotal'  => $date_rows['total'],
            "traceTotal" => $traces_data['total']
        );

        return $data;
    }

    /**
     * @param $id 文件id
     * @param $date 日期
     */
    private function from_date($id, $date) {
        //查询条件
        $condition = array("hidden"=>0,"date"=>$date);

        $traces_data = $this->file_trace_service->get_traces($id, $condition, $this->_trace_field);

        //没有轨迹
        if (empty($traces_data))
            return;

        $traces = $this->file_trace_service->traces_format($traces_data['rows'],0);

        return $traces;
    }

    /**
     * 格式化日期
     * @param $dates
     * @param int $start
     * @param int $active
     * @return array
     */
    private function date_format($dates,$start=0,$active=0)
    {
        $data = array();
        $temp = array();
        foreach($dates as $k=>$v)
        {
            $v['ts'] = date("Y-m-d",$v['ts']);
            if(in_array($v['ts'],$temp))
            {
                continue;
            }
            if($k == $active){
                $data[$k]['active'] = "active";
            }
            if(($k+1) % 2 == 0){
                $data[$k]['col2'] = "col2";
            }
            $data[$k]['index'] = $start+$k;
            $data[$k]['ts'] = $v['ts'];
            $temp[]=$v['ts'];
        }
        return array_values($data);
    }
}